package com.shuda.business.channel.controller;

import com.shuda.business.channel.dto.DownstreamChannelDTO;
import com.shuda.business.channel.dto.DownstreamChannelQueryDTO;
import com.shuda.business.channel.service.DownstreamChannelService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.common.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 下游渠道管理控制器
 */
@RestController
@RequestMapping("/api/channel/downstream")
@Tag(name = "下游渠道管理", description = "下游渠道管理接口")
public class DownstreamChannelController {

    @Autowired
    private DownstreamChannelService downstreamChannelService;

    @GetMapping("/page")
    @Operation(summary = "获取下游渠道分页列表")
    public Result<PageResult<DownstreamChannelDTO>> getDownstreamChannelPage(
            @Parameter(description = "分页参数") PageDTO pageDTO,
            @Parameter(description = "查询参数") DownstreamChannelQueryDTO queryDTO) {
        try {
            PageResult<DownstreamChannelDTO> result = downstreamChannelService.getDownstreamChannelPage(pageDTO, queryDTO);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取下游渠道详情")
    public Result<DownstreamChannelDTO> getDownstreamChannelById(
            @Parameter(description = "渠道ID") @PathVariable Long id) {
        try {
            DownstreamChannelDTO result = downstreamChannelService.getDownstreamChannelById(id);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "创建下游渠道")
    public Result<Long> createDownstreamChannel(
            @Parameter(description = "下游渠道信息") @RequestBody DownstreamChannelDTO dto) {
        try {
            Long id = downstreamChannelService.createDownstreamChannel(dto);
            return Result.success(id);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新下游渠道")
    public Result<Void> updateDownstreamChannel(
            @Parameter(description = "渠道ID") @PathVariable Long id,
            @Parameter(description = "下游渠道信息") @RequestBody DownstreamChannelDTO dto) {
        try {
            downstreamChannelService.updateDownstreamChannel(id, dto);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除下游渠道")
    public Result<Void> deleteDownstreamChannel(
            @Parameter(description = "渠道ID") @PathVariable Long id) {
        try {
            downstreamChannelService.deleteDownstreamChannel(id);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除下游渠道")
    public Result<Void> batchDeleteDownstreamChannel(
            @Parameter(description = "渠道ID列表") @RequestBody List<Long> ids) {
        try {
            downstreamChannelService.batchDeleteDownstreamChannel(ids);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新下游渠道状态")
    public Result<Void> updateDownstreamChannelStatus(
            @Parameter(description = "渠道ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam String status) {
        try {
            downstreamChannelService.updateDownstreamChannelStatus(id, status);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list/enabled")
    @Operation(summary = "获取启用的下游渠道列表")
    public Result<List<DownstreamChannelDTO>> getEnabledDownstreamChannelList() {
        try {
            List<DownstreamChannelDTO> result = downstreamChannelService.getEnabledDownstreamChannelList();
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }
}